package com.lemon.blog.web.controller;

import com.lemon.blog.web.utils.AliyunOSSUtil;
import com.lemon.common.core.web.controller.BaseController;
import com.lemon.common.core.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 包名称：com.lemon.admin.web.controller.common
 * 类名称：UploadController
 * 类描述：上传文件的接口
 * 创建人：@author huangpeng
 * 创建时间：2022年04月10日 01:01
 */
@Api("阿里云OSS文件上传")
@RestController
@RequestMapping("/upload")
public class UploadController extends BaseController {

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    // @Autowired
    // private ISysUserService userService;

    /**
     * 阿里云oss上传测试
     * @param file
     * @return Result<String>
     */
    @RequestMapping("/uploadFileTest")
    public AjaxResult uploadFileTest(MultipartFile file) throws IOException {
        // 设置上传文件夹
        String filePath = "test";
        // 文件上传
        String fileUrl = uploadFile(filePath, file);
        return AjaxResult.success("上传成功", fileUrl);
    }

    /**
     * 上传用户头像
     * @param file
     * @return Result<String>
     */
    @RequestMapping("/uploadUserHeader")
    public AjaxResult uploadUserHeader(MultipartFile file) throws IOException {
        // 设置上传文件夹
        String filePath = "lemon-user-header";
        // 文件上传
        String fileUrl = uploadFile(filePath, file);
        // 获取当前用户
        /*SysUser sysUser = userService.selectUserById(getUserId());
        sysUser.setAvatar(fileUrl);
        userService.updateUser(sysUser);*/
        return AjaxResult.success("上传成功", fileUrl);
    }

    /**
     * 上传文章封面
     * @param file
     * @return Result<String>
     */
    @RequestMapping("/uploadBlogCover")
    public AjaxResult uploadBlogCover(MultipartFile file) throws IOException {
        // 设置上传文件夹
        String filePath = "lemon-blog-cover";
        // 文件上传
        String fileUrl = uploadFile(filePath, file);
        return AjaxResult.success("上传成功", fileUrl);
    }

    /**
     * 上传文章封面
     * @param file
     * @return Result<String>
     */
    @RequestMapping("/uploadBlogImage")
    public AjaxResult uploadBlogImage(MultipartFile file) throws IOException {
        // 设置上传文件夹
        String filePath = "lemon-blog-image";
        // 文件上传
        String fileUrl = uploadFile(filePath, file);
        return AjaxResult.success("上传成功", fileUrl);

    }

    /**
     * 上传音乐
     * @param file
     * @return Result<String>
     */
    @RequestMapping("/uploadMusic")
    public AjaxResult uploadMusic(MultipartFile file) throws IOException{
        // 设置上传文件夹
        String filePath = "music";
        // 文件上传
        String fileUrl = uploadFile(filePath, file);
        return AjaxResult.success("上传成功", fileUrl);

    }

    /**
     * 文件上传
     * @param filePath
     * @param file
     * @return String
     */
    public String uploadFile(String filePath,MultipartFile file) throws IOException{
        // 获取文件后缀
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
        // 设置上传文件夹
        // String filePath = "music";
        // 设置文件名
        String objectName = filePath + "/" + UUID.randomUUID().toString() + "." + suffix;
        // 文件上传
        return aliyunOSSUtil.upload(file.getInputStream(),objectName);
    }
}
